package com.jeeplus.common.utils.security;



import javax.servlet.http.HttpServletRequest;



import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.Enumeration;


/**

 * Token的工具类

 * @author zhous

 * @since 2018-2-23 14:01:41

 *

 */

public class TokenTools {



    /**

     * 生成token放入session

     * @param request

     * @param tokenServerkey

     */

    public static String createToken(HttpServletRequest request,String tokenServerkey){

        String token = TokenProccessor.getInstance().makeToken();

        request.getSession().setAttribute(tokenServerkey, token);



        return token;
    }



    /**

     * 移除token

     * @param request

     * @param tokenServerkey

     */

    public static void removeToken(HttpServletRequest request,String tokenServerkey){

        request.getSession().removeAttribute(tokenServerkey);

    }


    /**

     * 判断请求参数中的token是否和session中一致

     * @param request

     * @param tokenClientkey

     * @param tokenServerkey

     * @return

     */

    public static boolean judgeTokenIsEqual(Logger logger, HttpServletRequest request, String tokenClientkey, String tokenServerkey){

        logger.debug("当前的tokenClientkey="+tokenClientkey);
//        String token_client = request.getParameter(tokenClientkey);
        if(StringUtils.isEmpty(tokenClientkey)){

            return false;

        }

        String token_server = (String) request.getSession().getAttribute(tokenServerkey);

        if(StringUtils.isEmpty(token_server)){

            return false;

        }

        logger.debug("yanruTODO 此时 tokenServerkey="+tokenServerkey+",token_server="+token_server
                +",token_server="+token_server
                +",不相同？"+(!token_server.equals(tokenClientkey))
        );


        if(!token_server.equals(tokenClientkey)){

            return false;

        }



        return true;

    }


    /**
     * 测试函数
     */
    public static void TestFunction()
    {

    }

}